//
//  CCNavigationControllerIMP.m
//  CCFoundation
//
//  Created by zjh on 2021/7/21.
//  Copyright © 2021 zjh. All rights reserved.
//

#import "CCNavigationController.h"
#import "NSBundle+CCFoundation.h"
#import "CCNavigationProtocol.h"

@interface CCNavigationController()<UIGestureRecognizerDelegate, UINavigationControllerDelegate>
@end

@implementation CCNavigationController
+ (void)initialize {
    UINavigationBar *navBar = [UINavigationBar appearance];
    [navBar setTranslucent:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationBar.shadowImage = [UIImage new];
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        __weak CCNavigationController *wsSelf = self;
        self.interactivePopGestureRecognizer.delegate = wsSelf;
        self.delegate = wsSelf;
    }
    // Do any additional setup after loading the view.
}

- (void)pushViewController:(UIViewController *)viewController
                  animated:(BOOL)animated {
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)] && animated == YES) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    if (self.childViewControllers.count) {
        UIBarButtonItem *leftBackItem  = [[UIBarButtonItem alloc] initWithImage:getFoundationImage(@"return_ic") style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
        viewController.navigationItem.leftBarButtonItems = @[leftBackItem];
        viewController.hidesBottomBarWhenPushed = YES;
        // 自动隐藏TabBar
    }
    [super pushViewController:viewController animated:animated];
}

//- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
//{
//    if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] && animated == YES )
//    {
//        self.interactivePopGestureRecognizer.enabled = NO;
//    }
//
//    return  [super popToRootViewControllerAnimated:animated];
//
//}
//
//- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    if( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] )
//    {
//        self.interactivePopGestureRecognizer.enabled = NO;
//    }
//
//    return [super popToViewController:viewController animated:animated];
//
//}

#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animate {
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.interactivePopGestureRecognizer.enabled = YES;
    }
}



- (nullable id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    if (operation == UINavigationControllerOperationPush) {
        id<CCNavgationAnimation> obj = (id<CCNavgationAnimation>)fromVC;
        if ([obj respondsToSelector:@selector(animationControllerForOperationPush)]) {
            return [obj animationControllerForOperationPush];
        }
    }
    else if(operation == UINavigationControllerOperationPop){
        id<CCNavgationAnimation> obj = (id<CCNavgationAnimation>)toVC;
        if ([obj respondsToSelector:@selector(animationControllerForOperationPop)]) {
            return [obj animationControllerForOperationPop];
        }
    }
    return nil;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        if ( self.viewControllers.count < 2 || self.visibleViewController == [self.viewControllers objectAtIndex:0] ) { ///第一控制器，不处理侧滑手势
            return NO;
        }
        
        if (self.visibleViewController != self.topViewController) return  NO; ///当前显示和最上层控制非统一视图时，不允许滑动；安全处理
        
        id<CCNavgationBarAppearnce> obj = (id<CCNavgationBarAppearnce>)self.topViewController;
        if ([obj respondsToSelector:@selector(sideslip)]) {
            return obj.sideslip;
        }
    }
    
    return YES;
}
#pragma mark - ---- Addition ----
- (void)backAction {
    //    [self.view endEditing:YES];//解决编辑状态黑影问题
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self popViewControllerAnimated:YES];
    //    });
}

#pragma mark - ---- StatusBar ----
- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.topViewController.preferredStatusBarStyle;
}

- (BOOL)prefersStatusBarHidden {
    return self.topViewController.prefersStatusBarHidden;
}
@end
